package com.example.flutter_bdface_plugin_example;

import android.app.Activity;
import android.util.Log;

import com.baidu.idl.face.platform.FaceConfig;
import com.baidu.idl.face.platform.FaceEnvironment;
import com.baidu.idl.face.platform.FaceSDKManager;
import com.baidu.idl.face.platform.LivenessTypeEnum;
import com.baidu.idl.face.platform.listener.IInitCallback;
import com.example.flutter_bdface_plugin.Config;
import com.example.flutter_bdface_plugin.FaceSDKResSettings;
import java.util.Set;

import io.flutter.app.FlutterApplication;

public class MainApplication extends FlutterApplication {
    
    @Override
    public void onCreate() {
        super.onCreate();
        initFacePlugin();
    }

    private void initFacePlugin() {
        // 初始化SDK
        FaceSDKManager.getInstance().initialize(this, "miaomumu-face-android",
                "idl-license.face-android", new IInitCallback() {
                    @Override
                    public void initSuccess() {
                        Log.e("baidu", "初始化成功");
                    }
                    @Override
                    public void initFailure(final int errCode, final String errMsg) {
                        Log.e("baidu", "初始化失败 = " + errCode + " " + errMsg);
                    }
                });
        // 随机动作
        Config.isLivenessRandom = true;
        // 根据需求添加活体动作
        Config.livenessList.clear();
        Config.livenessList.add(LivenessTypeEnum.Eye);
        // TODO test
//        Config.livenessList.add(LivenessTypeEnum.Mouth);
//        Config.livenessList.add(LivenessTypeEnum.HeadUp);
//        Config.livenessList.add(LivenessTypeEnum.HeadDown);
//        Config.livenessList.add(LivenessTypeEnum.HeadLeft);
//        Config.livenessList.add(LivenessTypeEnum.HeadRight);
//        Config.livenessList.add(LivenessTypeEnum.HeadLeftOrRight);
        // 设置 FaceConfig
        FaceConfig config = FaceSDKManager.getInstance()
                .getFaceConfig();
        // 设置可检测的最小人脸阈值
        config.setMinFaceSize(FaceEnvironment.VALUE_MIN_FACE_SIZE);
        // 设置可检测到人脸的阈值
        config.setNotFaceValue(FaceEnvironment.VALUE_NOT_FACE_THRESHOLD);
        // 设置模糊度阈值
        config.setBlurnessValue(FaceEnvironment.VALUE_BLURNESS);
        // 设置光照阈值（范围0-255）
        config.setBrightnessValue(FaceEnvironment.VALUE_BRIGHTNESS);
        // 设置遮挡阈值
        config.setOcclusionValue(FaceEnvironment.VALUE_OCCLUSION);
        // 设置人脸姿态角阈值
        config.setHeadPitchValue(FaceEnvironment.VALUE_HEAD_PITCH);
        config.setHeadYawValue(FaceEnvironment.VALUE_HEAD_YAW);
        // 设置闭眼阈值
        config.setEyeClosedValue(FaceEnvironment.VALUE_CLOSE_EYES);
        // 设置图片缓存数量
        config.setCacheImageNum(FaceEnvironment.VALUE_CACHE_IMAGE_NUM);
        // 设置活体动作，通过设置list，LivenessTypeEunm.Eye, LivenessTypeEunm.Mouth,
        // LivenessTypeEunm.HeadUp, LivenessTypeEunm.HeadDown, LivenessTypeEunm.HeadLeft,
        // LivenessTypeEunm.HeadRight, LivenessTypeEunm.HeadLeftOrRight
        config.setLivenessTypeList(Config.livenessList);
        // 设置动作活体是否随机
        config.setLivenessRandom(Config.isLivenessRandom);
        // 设置开启提示音
        config.setSound(true);
        // 原图缩放系数
        config.setScale(FaceEnvironment.VALUE_SCALE);
        // 抠图高的设定，为了保证好的抠图效果，我们要求高宽比是4：3，所以会在内部进行计算，只需要传入高即可
        config.setCropHeight(FaceEnvironment.VALUE_CROP_HEIGHT);
        // 加密类型，0：Base64加密，上传时image_sec传false；1：百度加密文件加密，上传时image_sec传true
        config.setSecType(FaceEnvironment.VALUE_SEC_TYPE);
        FaceSDKManager.getInstance().setFaceConfig(config);
        // 初始化资源文件
        FaceSDKResSettings.initializeResId();
    }
}
